﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace AdvanceLib.ClassExtensions
{
    public static class NumericExtensions
    {
        private static bool Parse(string input, ref long output)
        {
            try
            {
                if (input.IsHex()) output = Convert.ToInt64(input, 16);
                else if (input.IsDecimal()) output = Convert.ToInt64(input, 10);
                else return false;

                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool TryParse(this string self, ref long output)
        {
            return NumericExtensions.Parse(self, ref output);
        }
        public static bool IsHex(this string param)
        {
            bool result = true;
            string input = param.ToLower();
            char[] allowedCharset = new char[] {
                'a', 'b', 'c', 'd', 'e', 'f',
                '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
            };

            for (int i = 0; i < input.Length; i++)
            {
                switch (i)
                {
                    case 0:
                        result = input[i] == '0';
                        break;
                    case 1:
                        result = input[i] == 'x';
                        break;
                    default:
                        result = allowedCharset.Contains(input[i]);
                        break;
                }
                if (!result) break;
            }
            return result;
        }
        public static bool IsDecimal(this string param)
        {
            bool result = true;
            string input = param.ToLower();
            char[] allowedCharset = new char[] {
                '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
            };

            for (int i = 0; i < input.Length; i++)
            {
                result = allowedCharset.Contains(input[i]);
                if (!result) break;
            }
            return result;
        }
        public static bool IsPointerName(this string param)
        {
            bool result = true;
            string input = param.ToLower();
            char[] allowedCharset = new char[] {
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                '-', '.', '_',
                '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
            };

            for (int i = 0; i < input.Length; i++)
            {
                switch (i)
                {
                    case 0:
                        result = input[i] == '@';
                        break;
                    default:
                        result = allowedCharset.Contains(input[i]);
                        break;
                }
                if (!result) break;
            }
            return result;
        }
    }
}
